package com.xiaoshuidi.cloud.module.iot.uitils;

import java.util.Map;

/**
 * <br>
 *
 * @author Porter
 * create by 2019/7/16
 * @Emial Porter.Wang@ixiaoshuidi.com
 */
public class BiTeStringUtils {

    public static Map<String, Object> getSign(Map<String, Object> map) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            sb.append(stringBuilder(entry.getKey(), "=",
                    (entry.getValue() == null ? "" : String.valueOf(entry.getValue()))));
            sb.append("&");
        }
        String sign = sb.substring(0, sb.length() - 1);
        sign = BiTeMD5.GetMD5Code(sign);
        map.put("sign", sign);
        return map;
    }

    public static String stringBuilder(String... params) {
        StringBuilder sb = new StringBuilder();
        String[] var2 = params;
        int var3 = params.length;

        for (int var4 = 0; var4 < var3; ++var4) {
            String str = var2[var4];
            sb.append(str);
        }

        return sb.toString();
    }

    /**
     * 比特加密
     *
     * @param map
     * @return
     */
    public static Map encryptParam(Map<String, Object> map) {
        String token = (String) map.get("token");
        String pwd = token.substring(0, 25);
        for (String key : map.keySet()) {
            if ("time".equals(key) | "token".equals(key) | "sign".equals(key) | "version".equals(key)) {
                continue;
            }
            String value = String.valueOf(map.get(key));
            value = BiTeAES.encryptParam(pwd, value);
            map.put(key, value);
        }
        return map;
    }

    /**
     * 比特解密
     *
     * @param token
     * @param data
     * @return
     */
    public static String decryptParam(String token, String data) {
        String pwd = token.substring(0, 25);
        data = BiTeAES.decryptParam(pwd, data);
        return data;
    }
}
